<?php

namespace App\Jobs\Posts;

use App\Http\Controllers\Api\Repositories\WxCommentRepositores;
use App\Jobs\CalculateCommentLikesNumJob;
use App\Jobs\ImitatePostlikesStepJob;
use App\Models\Posts\WxPost;
use App\Models\Posts\WxPostsCommentManual;
use App\Wen\Utils\UserUtils;
use App\Wen\Utils\Utils;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldBeUnique;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use Illuminate\Support\Facades\Cache;

class AddPostCommentManuakJob implements ShouldQueue
{
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
    protected $comment_manual_id = 0;
    public $tries = 1;
    /**
     * Create a new job instance.
     *
     * @return void
     */
    public function __construct($comment_manual_id)
    {
        //
        $this->comment_manual_id = $comment_manual_id;
    }

    /**
     * Execute the job.
     *
     * @return void
     */
    public function handle()
    {
        //
        if(!_empty_($this->comment_manual_id)){
            $manual = WxPostsCommentManual::find($this->comment_manual_id);
            if($manual->status === 0){
                if(WxPost::where('id', $manual->posts_id)->where('is_examine', 1)->where('posts_state', 0)->exists()){
                    if($manual->expect_user > 0){
                        $user_id = $manual->expect_user;
                    }else{
                        $user_id = UserUtils::get_a_random_robot_uid();
                    }
                    if($user_id > 0){
                        $r = WxCommentRepositores::admin_add('', $manual->posts_id, 0,0, $user_id, $manual->content, $manual->img);
                        if($r){
                            WxPostsCommentManual::where('id', $manual->id)->update([
                                'status' => 1
                            ]);
                            Cache::forget('post:commentCount:'.$manual->posts_id);
                            UserUtils::add_user_experience($user_id, 2);
                            $comment_count = WxCommentRepositores::commentCount($manual->posts_id) ?: 0;//评论数量
                            $weight = WxPost::where('id', $manual->posts_id)->value('weight');
                            $weight += ( Utils::getDecayCoefficient($comment_count, 5) * 1000);
                            if($weight > time()){
                                $weight = time();
                            }
                            $post_id = $manual->posts_id;
                            WxPost::withoutSyncingToSearch(function () use ($post_id, $weight){
                                WxPost::where('id', $post_id)->update(['last_reply_at' => current_time(), 'weight'=>timetostr($weight)]);
                            });
                            CalculateCommentLikesNumJob::dispatch($r);
                            ImitatePostlikesStepJob::dispatch($manual->posts_id, mini_rand(1, 5))->delay(now()->addMinutes(mini_rand(1, 10)));
                        }
                    }
                }
            }
        }
    }
}
